// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    userInfo: '',
    index: 0,
    array: [4, 5, 6, 7, 8, 9, 10, 11, 12],
    // index1: 0,
    // array1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    myWord: '',
    total: 4,
    dintNum: 1,
    civilianNum: 3,
    roomNum: 0,
    content: '',
    showRoom: false,
    sendButtDisable: true,
    showInput: true,
    inputRoomNum: '',
    myNum: 1,
    // chooseDintNum: 1
  },

  clickMe(event) {
    var content = event.target.dataset.words;
    this.setData({
      content
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserInfo()
  },

  //获取用户信息
  getUserInfo() {
    //判断用户是否授权
    wx.getSetting({
      success: (data) => {
        console.log(data)
        if (data.authSetting['scope.userInfo']) {
          //用户已授权
          this.setData({
            isShow: false
          })
        } else {
          //未授权
          this.setData({
            isShow: true
          })
        }
      }
    })
    //获取用户登陆信息
    wx.getUserInfo({
      success: (data) => {
        //更新data中的userInfo
        console.log(data.userInfo);
        this.setData({
          userInfo: data.userInfo
        })

      },
      fail: (data) => {

      }
    })
  },
  handleGetUserInfo(data) {
    console.log(data)
    //判断用户是否点击了允许
    if (data.detail.rawData) {
      this.getUserInfo()
    }
  },
  //监听picker更换
  bindPickerChange(e) {
    let index = e.detail.value
    let total = this.data.array[index];
    let dintNum = Math.floor(total / 4);
    let civilianNum = total - dintNum;
    console.log('选择人数为' + total);
    this.setData({
      index,
      total,
      dintNum,
      civilianNum
    })
  },
  // bindPickerChange1(e) {
  //   let index1 = e.detail.value;
  //   let chooseDintNum = this.data.array1[index1];
  //   this.setData({
  //     index1,
  //     chooseDintNum
  //   })
  // },
  TypeInputRoom: function(e) {
    var inputRoomNum = e.detail.value;
    var buttDis = true;
    if (e.detail.value != "") {
      buttDis = false;
    } else {
      buttDis = true;
    }
    this.setData({
      sendButtDisable: buttDis,
      inputRoomNum
    })
  },
  //判断是否为卧底
  judge() {
    wx.request({
      url: 'https://www.haofanhudong.cn:8442/room/playerStatus',
      data: {
        room_id: this.data.roomNum,
        num: this.data.myNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (res) => {
        if (res.data.status == "error") {
          wx.showToast({
            title: '返回数据有误！',
          })
          return;
        }
        console.log(res);
       if(res.data.isStatus==0){
         wx.showToast({
           title: this.data.myNum+'号不是卧底',
         })
       } else
         wx.showToast({
           title: this.data.myNum + '号是卧底',
         })
       
      },
      fail: function (res) {
        wx.showToast({
          title: '请求失败！',
        })
        return;
      },
    })
  },
  //进入房间
  enterRoom() {
    wx.request({
      url: 'https://www.haofanhudong.cn:8442/room/addPlayer',
      data: {
        player_id: this.data.userInfo.avatarUrl,
        room_id: this.data.inputRoomNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (res) => {
        if (res.data.status == "error") {
          wx.showToast({
            title: '返回数据有误！',
          })
          return;
        }
        console.log(res);
        var resData = res.data;
        var myNum = resData.num;
        var myWord = resData.word;
        this.setData({
          myNum,
          myWord,
          showInput: false,
          roomNum: this.data.inputRoomNum,
          inputRoomNum: ''
        })

      },
      fail: function(res) {
        console.log(res);
        wx.showToast({
          title: '不存在该房间！',
        })
        return;
      },
    })
  },
  //通过手机输入法的方式点击发送房间号
  sendMessage: function(e) {
    if (e.detail.value != "") {
      this.enterRoom(e);

    }
  },
  //创建房间
  createRoom() {
    wx.request({
      url: 'https://www.haofanhudong.cn:8442/room/createRoom',
      data: {
        player_id: this.data.userInfo.avatarUrl,
        mole_num: this.data.dintNum,
        commoner_num: this.data.civilianNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: (res) => {
        if (res.data.status == "error") {
          wx.showToast({
            title: '返回数据有误！',
          })
          return;
        }
        console.log(res);
        var resData = res.data;
        var roomNum = resData.room_id;
        var myWord = resData.word;
        var myNum = resData.num;
        this.setData({
          myWord,
          roomNum,
          showRoom: true,
          myNum
        })

      },
      fail: function(res) {
        wx.showToast({
          title: '请求失败！',
        })
        return;
      },
    })

  },
  gameover() {
    this.setData({
      showRoom: false,
      showInput: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})